/*
 * Amkai Enterprise Solution (AES.) 
 * Copyright 2004-2008 by Amkai(TM), Inc.
 * www.amkai.com
 * 
 * Project: webservice
 * Package: com.amkai.aes.webservice.binary.objecthandler.billing
 * File: PeriodHandler.java
 * Created: 2008.11.04. 17:16:21
 * Author: polaj 
 * 
 */
package com.amkai.aes.webservice.binary.objecthandler.billing;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.amkai.aes.aescommon.service.binary.job.billing.PeriodJob;
import com.amkai.aes.aescommon.service.result.billing.PeriodResult;
import com.amkai.aes.generatedbeans.billinginterval.Period;
import com.amkai.aes.generatedbeans.organization.Department;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class PeriodHandler implements JobObjectHandler<PeriodJob, Department, PeriodResult>{

	private static final Log log = LogFactory.getLog(PeriodHandler.class);
	
	@Override
	public PeriodResult process(PeriodJob job, Department dep) throws Exception {
		switch (job.getAction()) {
		case GET_PERIODS:	return getPeriods(job,dep);
		}
		return null;
	}
	
	protected PeriodResult getPeriods(PeriodJob job, Department dep) {
		SessionManager sm = null;
		PeriodResult result = new PeriodResult();
		try {
			sm = SiteManager.Start();
			List<Period> l = sm.execute("FROM Period p WHERE p.baseOrganization=:bo").setParameter("bo", dep).list();
			result.setPeriods(l);
		}
		catch (Exception ex) {
			result.setException(ex);
			log.error("Error", ex);
		}
		finally {
			sm.closeSession();
		}
		return result;
	}
}
